﻿using System;
using System.IO;
using System.Threading.Tasks;

namespace _054_使用Parallel类执行并行操作
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] 文件名称 =
            {
                "demo_1_dx","demo_2_dx","demo_3_dx","demo_4_dx",
                "demo_5_dx","demo_6_dx","demo_7_dx","demo_8_dx"
            };
            Random 写作方法 = new Random();
            Parallel.ForEach(文件名称, (fn) =>
            {
                int 字段1;
                byte[] 字段;
                lock (写作方法)
                {
                    //随机产生文件长度
                    字段1 = 写作方法.Next(100, 9000);
                    字段 = new byte[字段1];
                    //生成随机字节序列
                    写作方法.NextBytes(字段);
                }

                using (FileStream 操作手机 = new FileStream(fn, FileMode.Create))
                {
                    操作手机.Write(字段);
                }
                Console.WriteLine($"已向{fn}写入{字段.Length}字节");
            });

        }
    }
}
